
import 'package:hm_show_flutter/api/api.dart';
import 'package:hm_show_flutter/constant/string.dart';
import 'package:hm_show_flutter/entity/company_entity.dart';
import 'package:hm_show_flutter/entity/home_entity.dart';
import 'package:hm_show_flutter/utils/http_util.dart';

typedef OnSuccessList<T>(List<T> list);

typedef OnFail(String message);

typedef OnSuccess<T>(T successData);

class HomeService {

  Future getHomeData(OnSuccess onSuccess, OnFail onFail) async {
    try {
//      Map<String, dynamic> params = id > 0
//          ? new FormData.from({
//              "companyId": id,
//            })
//          : null;
      var response = await HttpUtil.instance.get(Api.HOME);
      if (response['errno'] == 0) {
        HomeEntity homeEntity = HomeEntity.fromJson(response);
        onSuccess(homeEntity);
      } else {
        onFail(response['errmsg']);
      }
    } catch (e) {
      print(e);
      onFail(Strings.SERVER_EXCEPTION);
    }
  }

  Future getCompanyListData(OnSuccess onSuccess, OnFail onFail) async {
    try {
      var response = await HttpUtil.instance.get(Api.COMPANY_LIST_STORE);
      if (response['errno'] == 0) {
        CompanyEntity entity = CompanyEntity.fromJson(response);
        onSuccess(entity);
      } else {
        onFail(response['errmsg']);
      }
    } catch (e) {
      print(e);
      onFail(Strings.SERVER_EXCEPTION);
    }
  }
}
